#! /bin/sh
#
# yunion      Start up yunion clouds
#
# chkconfig: 2345 90 10
# description: Yunion cloud software suite
#

### BEGIN INIT INFO
# Provides:          yunion
# Required-Start:    $ALL
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Yunion clouds
# Description:       Yunion clouds
### END INIT INFO

# Author: Jian Qiu <qiujian@yunionyun.com>
#

# Do NOT "set -e"

function loaded() {
    systemctl status ${1}.service | grep Loaded | awk '{print $2}'
}

function do_start() {
  if [ -e /etc/sysconfig/yunionauth ] && [ ! -e /etc/kubernetes/kubelet.conf ] && [ ! -e /etc/yunion/host.conf ]; then

    . /etc/sysconfig/yunionauth

    if [ ! -z "$DOCKER_DAEMON_JSON" ]; then
        echo "$DOCKER_DAEMON_JSON" | base64 -d > /etc/docker/daemon.json
        systemctl restart docker
    fi

    if [ "$YUNION_START" == "yes" ]; then
      /opt/yunion/bin/ocadm join $API_SERVER \
          --enable-host-agent \
          --token $JOIN_TOKEN \
          --discovery-token-unsafe-skip-ca-verification
      exit 0
    fi

  fi
}

case "$1" in
  start)
    do_start
    ;;
  stop)
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop}" >&2
    exit 3
    ;;
esac

exit 0
